package pers.youqu.item.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pers.youqu.common.dto.PageDTO;
import pers.youqu.common.dto.QueryDTO;
import pers.youqu.item.dto.BrandDTO;
import pers.youqu.item.service.BrandService;

import java.util.List;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/12/5 14:22
 * @Description TODO
 */
@RestController
public class BrandController {

    private final BrandService brandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }

    /**
     * 根据id查询品牌
     *
     * @param id 品牌id
     * @return 品牌信息
     */
    @GetMapping("/brand/{id}")
    public ResponseEntity<BrandDTO> findBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(brandService.findBrandById(id));
    }

    /**
     * 根据品牌id集合查询品牌集合
     *
     * @param ids 品牌id集合
     * @return 品牌信息
     */
    @GetMapping("/brand/list")
    public ResponseEntity<List<BrandDTO>> findBrandsByIds(@RequestParam("ids") List<Long> ids) {
        System.out.println("ids = " + ids);
        return ResponseEntity.ok(brandService.findBrandsByIds(ids));
    }

    /**
     * 分页查询品牌
     *
     * @return 分页数据结果
     */
    @GetMapping("/brand/page")
    public ResponseEntity<PageDTO<BrandDTO>> findBrandsByPage(
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "5")Integer rows,
            @RequestParam(value = "key", required = false)String key
    ) {
        return ResponseEntity.ok(brandService.findBrandsByPage(page,rows,key));
    }

    /**
     * 根据分类id查询品牌
     *
     * @param id 分类id
     * @return 查询品牌结果
     */
    @GetMapping("/brand/of/category")
    public ResponseEntity<List<BrandDTO>> findBrandsByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(brandService.findBrandsByCategoryId(id));
    }


    /**
     * 新增品牌
     *
     * @param brandDTO 需要添加的品牌信息
     * @return 插入成功状态码
     */
    @PostMapping("/brand")
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO) {
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新品牌
     * @param brandDTO 需要修改的品牌信息
     * @return 修改成功状态码
     */
    @PutMapping("/brand")
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO) {
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据id删除品牌
     * @param id 品牌id
     * @return 删除状态码
     */
    @DeleteMapping("/brand/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long id) {
        brandService.deleteBrand(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
